package com.anyou.mall.platform.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.anyou.common.constant.RedisKeyConstant;
import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.core.redis.RedisCache;
import com.anyou.common.exception.GlobalException;
import com.anyou.common.utils.StringUtils;
import com.anyou.mall.platform.domain.PlaPlatformSettings;
import com.anyou.mall.platform.dto.*;
import com.anyou.mall.platform.vo.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.anyou.mall.platform.mapper.PlaPlatformSettingsMapper;
import com.anyou.mall.platform.service.IPlaPlatformSettingsService;

import java.util.List;

/**
 * 平台设置Service业务层处理
 *
 * @author ylj
 * @date 2022-03-21
 */
@Service
public class PlaPlatformSettingsServiceImpl extends ServiceImpl<PlaPlatformSettingsMapper, PlaPlatformSettings> implements IPlaPlatformSettingsService
{
    @Autowired
    private PlaPlatformSettingsMapper plaPlatformSettingsMapper;


    @Autowired
    private RedisCache redisCache;




    @Override
    public ApiResult<TopBackgroundVO> getTopBackground() {
        String result = redisCache.getCacheObject(RedisKeyConstant.REDIS_TOP_BACKGROUND_KEY);
        TopBackgroundVO vo = JSON.parseObject(result, new TypeReference<TopBackgroundVO>() {});
        if(vo == null){
            PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_TOP_BACKGROUND_KEY));
            if(cache != null){
                vo = JSON.parseObject(cache.getSettingsValue(), new TypeReference<TopBackgroundVO>() {});
                redisCache.setCacheObject(RedisKeyConstant.REDIS_TOP_BACKGROUND_KEY,cache.getSettingsValue());
            }
        }
        return ApiResult.success(vo);
    }

    @Override
    public ApiResult setTopBackground(TopBackgroundDTO dto, Long userId) {
        PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_TOP_BACKGROUND_KEY));
        if(cache == null){
            cache = new PlaPlatformSettings();
            cache.setSettingsKey(RedisKeyConstant.REDIS_TOP_BACKGROUND_KEY);
            cache.setSettingsValue(JSON.toJSONString(dto));
            cache.setSettingsName("首页顶部背景");
            cache.setCreateBy(userId.toString());
            cache.setUpdateBy(userId.toString());
            baseMapper.insert(cache);
        }else{
            cache.setUpdateBy(userId.toString());
            cache.setSettingsValue(JSON.toJSONString(dto));
            baseMapper.updateById(cache);
        }
        redisCache.setCacheObject(RedisKeyConstant.REDIS_TOP_BACKGROUND_KEY,JSON.toJSONString(dto));
        return ApiResult.success();
    }

    @Override
    public ApiResult<BottomIconVO> getBottomIcon() {
        String result = redisCache.getCacheObject(RedisKeyConstant.REDIS_BOTTOM_ICON_KEY);
        BottomIconVO vo = JSON.parseObject(result, new TypeReference<BottomIconVO>() {});
        if(vo == null){
            PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_BOTTOM_ICON_KEY));
            if(cache != null){
                vo = JSON.parseObject(cache.getSettingsValue(), new TypeReference<BottomIconVO>() {});
                redisCache.setCacheObject(RedisKeyConstant.REDIS_BOTTOM_ICON_KEY,cache.getSettingsValue());
            }
        }
        return ApiResult.success(vo);
    }

    @Override
    public ApiResult setBottomIcon(BottomIconDTO dto, Long userId) {
        PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_BOTTOM_ICON_KEY));
        if(cache == null){
            cache = new PlaPlatformSettings();
            cache.setSettingsKey(RedisKeyConstant.REDIS_BOTTOM_ICON_KEY);
            cache.setSettingsValue(JSON.toJSONString(dto));
            cache.setSettingsName("底部导航栏图标");
            cache.setCreateBy(userId.toString());
            cache.setUpdateBy(userId.toString());
            baseMapper.insert(cache);
        }else{
            cache.setUpdateBy(userId.toString());
            cache.setSettingsValue(JSON.toJSONString(dto));
            baseMapper.updateById(cache);
        }
        redisCache.setCacheObject(RedisKeyConstant.REDIS_BOTTOM_ICON_KEY,JSON.toJSONString(dto));
        return ApiResult.success();
    }

    @Override
    public ApiResult<BondVO> getBond() {
        String result = redisCache.getCacheObject(RedisKeyConstant.REDIS_BOND_KEY);
        BondVO vo = JSON.parseObject(result, new TypeReference<BondVO>() {});
        if(vo == null){
            PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_BOND_KEY));
            if(cache != null){
                vo = JSON.parseObject(cache.getSettingsValue(), new TypeReference<BondVO>() {});
                redisCache.setCacheObject(RedisKeyConstant.REDIS_BOND_KEY,cache.getSettingsValue());
            }
        }
        return ApiResult.success(vo);
    }

    @Override
    public ApiResult setBond(BondDTO dto, Long userId) {
        PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_BOND_KEY));
        if(cache == null){
            cache = new PlaPlatformSettings();
            cache.setSettingsKey(RedisKeyConstant.REDIS_BOND_KEY);
            cache.setSettingsValue(JSON.toJSONString(dto));
            cache.setSettingsName("保证金相关");
            cache.setCreateBy(userId.toString());
            cache.setUpdateBy(userId.toString());
            baseMapper.insert(cache);
        }else{
            cache.setUpdateBy(userId.toString());
            cache.setSettingsValue(JSON.toJSONString(dto));
            baseMapper.updateById(cache);
        }
        redisCache.setCacheObject(RedisKeyConstant.REDIS_BOND_KEY,JSON.toJSONString(dto));
        return ApiResult.success();
    }

    @Override
    public ApiResult<AboutUsVO> getAboutUs() {
        String result = redisCache.getCacheObject(RedisKeyConstant.REDIS_ABOUT_US_KEY);
        AboutUsVO vo = JSON.parseObject(result, new TypeReference<AboutUsVO>() {});
        if(vo == null){
            PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_ABOUT_US_KEY));
            if(cache != null){
                vo = JSON.parseObject(cache.getSettingsValue(), new TypeReference<AboutUsVO>() {});
                redisCache.setCacheObject(RedisKeyConstant.REDIS_ABOUT_US_KEY,cache.getSettingsValue());
            }
        }
        return ApiResult.success(vo);
    }

    @Override
    public ApiResult setAboutUs(AboutUsDTO dto, Long userId) {
        PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_ABOUT_US_KEY));
        if(cache == null){
            cache = new PlaPlatformSettings();
            cache.setSettingsKey(RedisKeyConstant.REDIS_ABOUT_US_KEY);
            cache.setSettingsValue(JSON.toJSONString(dto));
            cache.setSettingsName("关于我们");
            cache.setCreateBy(userId.toString());
            cache.setUpdateBy(userId.toString());
            baseMapper.insert(cache);
        }else{
            cache.setUpdateBy(userId.toString());
            cache.setSettingsValue(JSON.toJSONString(dto));
            baseMapper.updateById(cache);
        }
        redisCache.setCacheObject(RedisKeyConstant.REDIS_ABOUT_US_KEY,JSON.toJSONString(dto));
        return ApiResult.success();
    }

    @Override
    public ApiResult getUserAgreementInfo() {
        String userAgreementInfo = redisCache.getCacheObject(RedisKeyConstant.REDIS_USER_AGREEMENT_INFO_KEY);
        if(userAgreementInfo == null){
            PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_USER_AGREEMENT_INFO_KEY));
            if(cache != null){
                userAgreementInfo = cache.getSettingsValue();
                redisCache.setCacheObject(RedisKeyConstant.REDIS_USER_AGREEMENT_INFO_KEY,userAgreementInfo);
            }
        }
        return ApiResult.success(userAgreementInfo);
    }

    @Override
    public ApiResult setUserAgreementInfo(String userAgreementInfo, Long userId) {
        PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_USER_AGREEMENT_INFO_KEY));
        if(cache == null){
            cache = new PlaPlatformSettings();
            cache.setSettingsKey(RedisKeyConstant.REDIS_USER_AGREEMENT_INFO_KEY);
            cache.setSettingsValue(userAgreementInfo);
            cache.setSettingsName("用户协议");
            cache.setCreateBy(userId.toString());
            cache.setUpdateBy(userId.toString());
            baseMapper.insert(cache);
        }else{
            cache.setUpdateBy(userId.toString());
            cache.setSettingsValue(userAgreementInfo);
            baseMapper.updateById(cache);
        }
        redisCache.setCacheObject(RedisKeyConstant.REDIS_USER_AGREEMENT_INFO_KEY,userAgreementInfo);
        return ApiResult.success();
    }

    @Override
    public ApiResult getPrivacyPolicy() {
        String privacyPolicy="";
        PlaPlatformSettings redis_privacy_policy = plaPlatformSettingsMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>()
                .eq(PlaPlatformSettings::getSettingsKey, "redis_privacy_policy"));
        if(redis_privacy_policy!=null&& StringUtils.isNotEmpty(redis_privacy_policy.getSettingsValue())){
            privacyPolicy=redis_privacy_policy.getSettingsValue();
        }
//        String privacyPolicy = redisCache.getCacheObject(RedisKeyConstant.REDIS_PRIVACY_POLICY_KEY);
//        if(privacyPolicy == null){
//            PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_PRIVACY_POLICY_KEY));
//            if(cache != null){
//                privacyPolicy = cache.getSettingsValue();
//                redisCache.setCacheObject(RedisKeyConstant.REDIS_PRIVACY_POLICY_KEY,privacyPolicy);
//            }
//        }
        return ApiResult.success(privacyPolicy);
    }

    @Override
    public ApiResult setPrivacyPolicy(String privacyPolicy, Long userId) {
        PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_PRIVACY_POLICY_KEY));
        if(cache == null){
            cache = new PlaPlatformSettings();
            cache.setSettingsKey(RedisKeyConstant.REDIS_PRIVACY_POLICY_KEY);
            cache.setSettingsValue(privacyPolicy);
            cache.setSettingsName("隐私政策");
            cache.setCreateBy(userId.toString());
            cache.setUpdateBy(userId.toString());
            baseMapper.insert(cache);
        }else{
            cache.setUpdateBy(userId.toString());
            cache.setSettingsValue(privacyPolicy);
            baseMapper.updateById(cache);
        }
        redisCache.setCacheObject(RedisKeyConstant.REDIS_PRIVACY_POLICY_KEY,privacyPolicy);
        return ApiResult.success();
    }

    @Override
    public ApiResult getAutoPartsSettledAgreement() {
        String autoPartsSettledAgreement = redisCache.getCacheObject(RedisKeyConstant.REDIS_AUTO_PARTS_SETTLED_AGREEMENT_KEY);
        if(autoPartsSettledAgreement == null){
            PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_AUTO_PARTS_SETTLED_AGREEMENT_KEY));
            if(cache != null){
                autoPartsSettledAgreement = cache.getSettingsValue();
                redisCache.setCacheObject(RedisKeyConstant.REDIS_AUTO_PARTS_SETTLED_AGREEMENT_KEY,autoPartsSettledAgreement);
            }
        }
        return ApiResult.success(autoPartsSettledAgreement);
    }

    @Override
    public ApiResult setAutoPartsSettledAgreement(String autoPartsSettledAgreement, Long userId) {
        PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_AUTO_PARTS_SETTLED_AGREEMENT_KEY));
        if(cache == null){
            cache = new PlaPlatformSettings();
            cache.setSettingsKey(RedisKeyConstant.REDIS_AUTO_PARTS_SETTLED_AGREEMENT_KEY);
            cache.setSettingsValue(autoPartsSettledAgreement);
            cache.setSettingsName("汽配商入驻协议");
            cache.setCreateBy(userId.toString());
            cache.setUpdateBy(userId.toString());
            baseMapper.insert(cache);
        }else{
            cache.setUpdateBy(userId.toString());
            cache.setSettingsValue(autoPartsSettledAgreement);
            baseMapper.updateById(cache);
        }
        redisCache.setCacheObject(RedisKeyConstant.REDIS_AUTO_PARTS_SETTLED_AGREEMENT_KEY,autoPartsSettledAgreement);
        return ApiResult.success();
    }

    @Override
    public ApiResult getAutoPartsPaymentAgreement() {
        String autoPartsPaymentAgreement = redisCache.getCacheObject(RedisKeyConstant.REDIS_AUTO_PARTS_PAYMENT_AGREEMENT_KEY);
        if(autoPartsPaymentAgreement == null){
            PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_AUTO_PARTS_PAYMENT_AGREEMENT_KEY));
            if(cache != null){
                autoPartsPaymentAgreement = cache.getSettingsValue();
                redisCache.setCacheObject(RedisKeyConstant.REDIS_AUTO_PARTS_PAYMENT_AGREEMENT_KEY,autoPartsPaymentAgreement);
            }
        }
        return ApiResult.success(autoPartsPaymentAgreement);
    }

    @Override
    public ApiResult setAutoPartsPaymentAgreement(String autoPartsPaymentAgreement, Long userId) {
        PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_AUTO_PARTS_PAYMENT_AGREEMENT_KEY));
        if(cache == null){
            cache = new PlaPlatformSettings();
            cache.setSettingsKey(RedisKeyConstant.REDIS_AUTO_PARTS_PAYMENT_AGREEMENT_KEY);
            cache.setSettingsValue(autoPartsPaymentAgreement);
            cache.setSettingsName("汽配商保证金支付协议和开店协议");
            cache.setCreateBy(userId.toString());
            cache.setUpdateBy(userId.toString());
            baseMapper.insert(cache);
        }else{
            cache.setUpdateBy(userId.toString());
            cache.setSettingsValue(autoPartsPaymentAgreement);
            baseMapper.updateById(cache);
        }
        redisCache.setCacheObject(RedisKeyConstant.REDIS_AUTO_PARTS_PAYMENT_AGREEMENT_KEY,autoPartsPaymentAgreement);
        return ApiResult.success();
    }

    @Override
    public ApiResult getRepairSettledAgreement() {
        String repairSettledAgreement = redisCache.getCacheObject(RedisKeyConstant.REDIS_REPAIR_SETTLED_AGREEMENT_KEY);
        if(repairSettledAgreement == null){
            PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_REPAIR_SETTLED_AGREEMENT_KEY));
            if(cache != null){
                repairSettledAgreement = cache.getSettingsValue();
                redisCache.setCacheObject(RedisKeyConstant.REDIS_REPAIR_SETTLED_AGREEMENT_KEY,repairSettledAgreement);
            }
        }
        return ApiResult.success(repairSettledAgreement);
    }

    @Override
    public ApiResult setRepairSettledAgreement(String repairSettledAgreement, Long userId) {
        PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_REPAIR_SETTLED_AGREEMENT_KEY));
        if(cache == null){
            cache = new PlaPlatformSettings();
            cache.setSettingsKey(RedisKeyConstant.REDIS_REPAIR_SETTLED_AGREEMENT_KEY);
            cache.setSettingsValue(repairSettledAgreement);
            cache.setSettingsName("维修厂入驻协议");
            cache.setCreateBy(userId.toString());
            cache.setUpdateBy(userId.toString());
            baseMapper.insert(cache);
        }else{
            cache.setUpdateBy(userId.toString());
            cache.setSettingsValue(repairSettledAgreement);
            baseMapper.updateById(cache);
        }
        redisCache.setCacheObject(RedisKeyConstant.REDIS_REPAIR_SETTLED_AGREEMENT_KEY,repairSettledAgreement);
        return ApiResult.success();
    }

    @Override
    public ApiResult getRepairPaymentAgreement() {
        String repairPaymentAgreement = redisCache.getCacheObject(RedisKeyConstant.REDIS_REPAIR_PAYMENT_AGREEMENT_KEY);
        if(repairPaymentAgreement == null){
            PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_REPAIR_PAYMENT_AGREEMENT_KEY));
            if(cache != null){
                repairPaymentAgreement = cache.getSettingsValue();
                redisCache.setCacheObject(RedisKeyConstant.REDIS_REPAIR_PAYMENT_AGREEMENT_KEY,repairPaymentAgreement);
            }
        }
        return ApiResult.success(repairPaymentAgreement);
    }

    @Override
    public ApiResult setRepairPaymentAgreement(String repairPaymentAgreement, Long userId) {
        PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_REPAIR_PAYMENT_AGREEMENT_KEY));
        if(cache == null){
            cache = new PlaPlatformSettings();
            cache.setSettingsKey(RedisKeyConstant.REDIS_REPAIR_PAYMENT_AGREEMENT_KEY);
            cache.setSettingsValue(repairPaymentAgreement);
            cache.setSettingsName("维修厂保证金支付协议");
            cache.setCreateBy(userId.toString());
            cache.setUpdateBy(userId.toString());
            baseMapper.insert(cache);
        }else{
            cache.setUpdateBy(userId.toString());
            cache.setSettingsValue(repairPaymentAgreement);
            baseMapper.updateById(cache);
        }
        redisCache.setCacheObject(RedisKeyConstant.REDIS_REPAIR_PAYMENT_AGREEMENT_KEY,repairPaymentAgreement);
        return ApiResult.success();
    }

    @Override
    public ApiResult getDriverJoinAgreement() {
        String driverJoinAgreement = redisCache.getCacheObject(RedisKeyConstant.REDIS_DRIVER_JOIN_AGREEMENT_KEY);
        if(driverJoinAgreement == null){
            PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_DRIVER_JOIN_AGREEMENT_KEY));
            if(cache != null){
                driverJoinAgreement = cache.getSettingsValue();
                redisCache.setCacheObject(RedisKeyConstant.REDIS_DRIVER_JOIN_AGREEMENT_KEY,driverJoinAgreement);
            }
        }
        return ApiResult.success(driverJoinAgreement);
    }

    @Override
    public ApiResult setDriverJoinAgreement(String driverJoinAgreement, Long userId) {
        PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_DRIVER_JOIN_AGREEMENT_KEY));
        if(cache == null){
            cache = new PlaPlatformSettings();
            cache.setSettingsKey(RedisKeyConstant.REDIS_DRIVER_JOIN_AGREEMENT_KEY);
            cache.setSettingsValue(driverJoinAgreement);
            cache.setSettingsName("司机申请加盟协议");
            cache.setCreateBy(userId.toString());
            cache.setUpdateBy(userId.toString());
            baseMapper.insert(cache);
        }else{
            cache.setUpdateBy(userId.toString());
            cache.setSettingsValue(driverJoinAgreement);
            baseMapper.updateById(cache);
        }
        redisCache.setCacheObject(RedisKeyConstant.REDIS_DRIVER_JOIN_AGREEMENT_KEY,driverJoinAgreement);
        return ApiResult.success();
    }

    @Override
    public ApiResult getBusinessPayAgreement() {
        String businessPayAgreement = redisCache.getCacheObject(RedisKeyConstant.REDIS_BUSINESS_PAY_AGREEMENT_KEY);
        if(businessPayAgreement == null){
            PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_BUSINESS_PAY_AGREEMENT_KEY));
            if(cache != null){
                businessPayAgreement = cache.getSettingsValue();
                redisCache.setCacheObject(RedisKeyConstant.REDIS_BUSINESS_PAY_AGREEMENT_KEY,businessPayAgreement);
            }
        }
        return ApiResult.success(businessPayAgreement);
    }

    @Override
    public ApiResult setBusinessPayAgreement(String businessPayAgreement, Long userId) {
        PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_BUSINESS_PAY_AGREEMENT_KEY));
        if(cache == null){
            cache = new PlaPlatformSettings();
            cache.setSettingsKey(RedisKeyConstant.REDIS_BUSINESS_PAY_AGREEMENT_KEY);
            cache.setSettingsValue(businessPayAgreement);
            cache.setSettingsName("商家会员缴费协议");
            cache.setCreateBy(userId.toString());
            cache.setUpdateBy(userId.toString());
            baseMapper.insert(cache);
        }else{
            cache.setUpdateBy(userId.toString());
            cache.setSettingsValue(businessPayAgreement);
            baseMapper.updateById(cache);
        }
        redisCache.setCacheObject(RedisKeyConstant.REDIS_BUSINESS_PAY_AGREEMENT_KEY,businessPayAgreement);
        return ApiResult.success();
    }

    @Override
    public ApiResult<DistributionPriceVO> getMinibusDistributionPrice() {
        String result = redisCache.getCacheObject(RedisKeyConstant.REDIS_MINIBUS_DISTRIBUTION_PRICE_KEY);
        DistributionPriceVO vo = JSON.parseObject(result, new TypeReference<DistributionPriceVO>() {});
        if(vo == null){
            PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_MINIBUS_DISTRIBUTION_PRICE_KEY));
            if(cache != null){
                vo = JSON.parseObject(cache.getSettingsValue(), new TypeReference<DistributionPriceVO>() {});
                redisCache.setCacheObject(RedisKeyConstant.REDIS_MINIBUS_DISTRIBUTION_PRICE_KEY,cache.getSettingsValue());
            }
        }
        return ApiResult.success(vo);
    }

    @Override
    public ApiResult setMinibusDistributionPrice(DistributionPriceDTO dto, Long userId) {
        PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_MINIBUS_DISTRIBUTION_PRICE_KEY));
        if(cache == null){
            cache = new PlaPlatformSettings();
            cache.setSettingsKey(RedisKeyConstant.REDIS_MINIBUS_DISTRIBUTION_PRICE_KEY);
            cache.setSettingsValue(JSON.toJSONString(dto));
            cache.setSettingsName("面包车配送费用");
            cache.setCreateBy(userId.toString());
            cache.setUpdateBy(userId.toString());
            baseMapper.insert(cache);
        }else{
            cache.setUpdateBy(userId.toString());
            cache.setSettingsValue(JSON.toJSONString(dto));
            baseMapper.updateById(cache);
        }
        redisCache.setCacheObject(RedisKeyConstant.REDIS_MINIBUS_DISTRIBUTION_PRICE_KEY,JSON.toJSONString(dto));
        return ApiResult.success();
    }

    @Override
    public ApiResult<DistributionPriceVO> getMinivanDistributionPrice() {
        String result = redisCache.getCacheObject(RedisKeyConstant.REDIS_MINIVAN_DISTRIBUTION_PRICE_KEY);
        DistributionPriceVO vo = JSON.parseObject(result, new TypeReference<DistributionPriceVO>() {});
        if(vo == null){
            PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_MINIVAN_DISTRIBUTION_PRICE_KEY));
            if(cache != null){
                vo = JSON.parseObject(cache.getSettingsValue(), new TypeReference<DistributionPriceVO>() {});
                redisCache.setCacheObject(RedisKeyConstant.REDIS_MINIVAN_DISTRIBUTION_PRICE_KEY,cache.getSettingsValue());
            }
        }
        return ApiResult.success(vo);
    }

    @Override
    public ApiResult setMinivanDistributionPrice(DistributionPriceDTO dto, Long userId) {
        PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_MINIVAN_DISTRIBUTION_PRICE_KEY));
        if(cache == null){
            cache = new PlaPlatformSettings();
            cache.setSettingsKey(RedisKeyConstant.REDIS_MINIVAN_DISTRIBUTION_PRICE_KEY);
            cache.setSettingsValue(JSON.toJSONString(dto));
            cache.setSettingsName("小货车配送费用");
            cache.setCreateBy(userId.toString());
            cache.setUpdateBy(userId.toString());
            baseMapper.insert(cache);
        }else{
            cache.setUpdateBy(userId.toString());
            cache.setSettingsValue(JSON.toJSONString(dto));
            baseMapper.updateById(cache);
        }
        redisCache.setCacheObject(RedisKeyConstant.REDIS_MINIVAN_DISTRIBUTION_PRICE_KEY,JSON.toJSONString(dto));
        return ApiResult.success();
    }

    @Override
    public ApiResult<DistributionPriceVO> getMotorcycleDistributionPrice() {
        String result = redisCache.getCacheObject(RedisKeyConstant.REDIS_MOTORCYCLE_DISTRIBUTION_PRICE_KEY);
        DistributionPriceVO vo = JSON.parseObject(result, new TypeReference<DistributionPriceVO>() {});
        if(vo == null){
            PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_MOTORCYCLE_DISTRIBUTION_PRICE_KEY));
            if(cache != null){
                vo = JSON.parseObject(cache.getSettingsValue(), new TypeReference<DistributionPriceVO>() {});
                redisCache.setCacheObject(RedisKeyConstant.REDIS_MOTORCYCLE_DISTRIBUTION_PRICE_KEY,cache.getSettingsValue());
            }
        }
        return ApiResult.success(vo);
    }

    @Override
    public ApiResult setMotorcycleDistributionPrice(DistributionPriceDTO dto, Long userId) {
        PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_MOTORCYCLE_DISTRIBUTION_PRICE_KEY));
        if(cache == null){
            cache = new PlaPlatformSettings();
            cache.setSettingsKey(RedisKeyConstant.REDIS_MOTORCYCLE_DISTRIBUTION_PRICE_KEY);
            cache.setSettingsValue(JSON.toJSONString(dto));
            cache.setSettingsName("摩托车配送费用");
            cache.setCreateBy(userId.toString());
            cache.setUpdateBy(userId.toString());
            baseMapper.insert(cache);
        }else{
            cache.setUpdateBy(userId.toString());
            cache.setSettingsValue(JSON.toJSONString(dto));
            baseMapper.updateById(cache);
        }
        redisCache.setCacheObject(RedisKeyConstant.REDIS_MOTORCYCLE_DISTRIBUTION_PRICE_KEY,JSON.toJSONString(dto));
        return ApiResult.success();
    }

    @Override
    public ApiResult<DistributionPriceVO> getBicycleDistributionPrice() {
        String result = redisCache.getCacheObject(RedisKeyConstant.REDIS_BICYCLE_DISTRIBUTION_PRICE_KEY);
        DistributionPriceVO vo = JSON.parseObject(result, new TypeReference<DistributionPriceVO>() {});
        if(vo == null){
            PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_BICYCLE_DISTRIBUTION_PRICE_KEY));
            if(cache != null){
                vo = JSON.parseObject(cache.getSettingsValue(), new TypeReference<DistributionPriceVO>() {});
                redisCache.setCacheObject(RedisKeyConstant.REDIS_BICYCLE_DISTRIBUTION_PRICE_KEY,cache.getSettingsValue());
            }
        }
        return ApiResult.success(vo);
    }

    @Override
    public ApiResult setBicycleDistributionPrice(DistributionPriceDTO dto, Long userId) {
        PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_BICYCLE_DISTRIBUTION_PRICE_KEY));
        if(cache == null){
            cache = new PlaPlatformSettings();
            cache.setSettingsKey(RedisKeyConstant.REDIS_BICYCLE_DISTRIBUTION_PRICE_KEY);
            cache.setSettingsValue(JSON.toJSONString(dto));
            cache.setSettingsName("单车配送费用");
            cache.setCreateBy(userId.toString());
            cache.setUpdateBy(userId.toString());
            baseMapper.insert(cache);
        }else{
            cache.setUpdateBy(userId.toString());
            cache.setSettingsValue(JSON.toJSONString(dto));
            baseMapper.updateById(cache);
        }
        redisCache.setCacheObject(RedisKeyConstant.REDIS_BICYCLE_DISTRIBUTION_PRICE_KEY,JSON.toJSONString(dto));
        return ApiResult.success();
    }

    @Override
    public ApiResult<DistributionPriceVO> getExceedWeightPrice() {
        String result = redisCache.getCacheObject(RedisKeyConstant.REDIS_EXCEED_WEIGHT_PRICE_KEY);
        DistributionPriceVO vo = JSON.parseObject(result, new TypeReference<DistributionPriceVO>() {});
        if(vo == null){
            PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_EXCEED_WEIGHT_PRICE_KEY));
            if(cache != null){
                vo = JSON.parseObject(cache.getSettingsValue(), new TypeReference<DistributionPriceVO>() {});
                redisCache.setCacheObject(RedisKeyConstant.REDIS_EXCEED_WEIGHT_PRICE_KEY,cache.getSettingsValue());
            }
        }
        return ApiResult.success(vo);
    }

    @Override
    public ApiResult setExceedWeightPrice(DistributionPriceDTO dto, Long userId) {
        PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_EXCEED_WEIGHT_PRICE_KEY));
        if(cache == null){
            cache = new PlaPlatformSettings();
            cache.setSettingsKey(RedisKeyConstant.REDIS_EXCEED_WEIGHT_PRICE_KEY);
            cache.setSettingsValue(JSON.toJSONString(dto));
            cache.setSettingsName("超重人工费用");
            cache.setCreateBy(userId.toString());
            cache.setUpdateBy(userId.toString());
            baseMapper.insert(cache);
        }else{
            cache.setUpdateBy(userId.toString());
            cache.setSettingsValue(JSON.toJSONString(dto));
            baseMapper.updateById(cache);
        }
        redisCache.setCacheObject(RedisKeyConstant.REDIS_EXCEED_WEIGHT_PRICE_KEY,JSON.toJSONString(dto));
        return ApiResult.success();
    }

    @Override
    public ApiResult<DistributionPriceVO> getExceedVolumePrice() {
        String result = redisCache.getCacheObject(RedisKeyConstant.REDIS_EXCEED_VOLUME_PRICE_KEY);
        DistributionPriceVO vo = JSON.parseObject(result, new TypeReference<DistributionPriceVO>() {});
        if(vo == null){
            PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_EXCEED_VOLUME_PRICE_KEY));
            if(cache != null){
                vo = JSON.parseObject(cache.getSettingsValue(), new TypeReference<DistributionPriceVO>() {});
                redisCache.setCacheObject(RedisKeyConstant.REDIS_EXCEED_VOLUME_PRICE_KEY,cache.getSettingsValue());
            }
        }
        return ApiResult.success(vo);
    }

    @Override
    public ApiResult setExceedVolumePrice(DistributionPriceDTO dto, Long userId) {
        PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_EXCEED_VOLUME_PRICE_KEY));
        if(cache == null){
            cache = new PlaPlatformSettings();
            cache.setSettingsKey(RedisKeyConstant.REDIS_EXCEED_VOLUME_PRICE_KEY);
            cache.setSettingsValue(JSON.toJSONString(dto));
            cache.setSettingsName("超体积人工费用");
            cache.setCreateBy(userId.toString());
            cache.setUpdateBy(userId.toString());
            baseMapper.insert(cache);
        }else{
            cache.setUpdateBy(userId.toString());
            cache.setSettingsValue(JSON.toJSONString(dto));
            baseMapper.updateById(cache);
        }
        redisCache.setCacheObject(RedisKeyConstant.REDIS_EXCEED_VOLUME_PRICE_KEY,JSON.toJSONString(dto));
        return ApiResult.success();
    }

    @Override
    public ApiResult getDistanceLimit() {
        String businessPayAgreement = redisCache.getCacheObject(RedisKeyConstant.REDIS_DISTANCE_LIMIT_KEY);
        if(businessPayAgreement == null){
            PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_DISTANCE_LIMIT_KEY));
            if(cache != null){
                businessPayAgreement = cache.getSettingsValue();
                redisCache.setCacheObject(RedisKeyConstant.REDIS_DISTANCE_LIMIT_KEY,businessPayAgreement);
            }
        }
        return ApiResult.success(businessPayAgreement);
    }

    @Override
    public ApiResult setDistanceLimit(String distanceLimit, Long userId) {
        PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_DISTANCE_LIMIT_KEY));
        if(cache == null){
            cache = new PlaPlatformSettings();
            cache.setSettingsKey(RedisKeyConstant.REDIS_DISTANCE_LIMIT_KEY);
            cache.setSettingsValue(distanceLimit);
            cache.setSettingsName("距离限制");
            cache.setCreateBy(userId.toString());
            cache.setUpdateBy(userId.toString());
            baseMapper.insert(cache);
        }else{
            cache.setUpdateBy(userId.toString());
            cache.setSettingsValue(distanceLimit);
            baseMapper.updateById(cache);
        }
        redisCache.setCacheObject(RedisKeyConstant.REDIS_DISTANCE_LIMIT_KEY,distanceLimit);
        return ApiResult.success();
    }

    @Override
    public ApiResult<StarMemberVO> getStarMember() {
        String result = redisCache.getCacheObject(RedisKeyConstant.REDIS_STAR_MEMBER_KEY);
        StarMemberVO vo = JSON.parseObject(result, new TypeReference<StarMemberVO>() {});
        if(vo == null){
            PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_STAR_MEMBER_KEY));
            if(cache != null){
                vo = JSON.parseObject(cache.getSettingsValue(), new TypeReference<StarMemberVO>() {});
                redisCache.setCacheObject(RedisKeyConstant.REDIS_STAR_MEMBER_KEY,cache.getSettingsValue());
            }
        }
        return ApiResult.success(vo);
    }

    @Override
    public ApiResult setStarMember(StarMemberDTO dto, Long userId) {
        PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_STAR_MEMBER_KEY));
        if(cache == null){
            cache = new PlaPlatformSettings();
            cache.setSettingsKey(RedisKeyConstant.REDIS_STAR_MEMBER_KEY);
            cache.setSettingsValue(JSON.toJSONString(dto));
            cache.setSettingsName("星级会员");
            cache.setCreateBy(userId.toString());
            cache.setUpdateBy(userId.toString());
            baseMapper.insert(cache);
        }else{
            cache.setUpdateBy(userId.toString());
            cache.setSettingsValue(JSON.toJSONString(dto));
            baseMapper.updateById(cache);
        }
        redisCache.setCacheObject(RedisKeyConstant.REDIS_STAR_MEMBER_KEY,JSON.toJSONString(dto));
        return ApiResult.success();
    }

    @Override
    public ApiResult getCommission() {
        String businessPayAgreement = redisCache.getCacheObject(RedisKeyConstant.REDIS_COMMISSION_KEY);
        if(businessPayAgreement == null){
            PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_COMMISSION_KEY));
            if(cache != null){
                businessPayAgreement = cache.getSettingsValue();
                redisCache.setCacheObject(RedisKeyConstant.REDIS_COMMISSION_KEY,businessPayAgreement);
            }
        }
        return ApiResult.success(businessPayAgreement);
    }

    @Override
    public ApiResult setCommission(String commission, Long userId) {
        PlaPlatformSettings cache = baseMapper.selectOne(new LambdaQueryWrapper<PlaPlatformSettings>().eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_COMMISSION_KEY));
        if(cache == null){
            cache = new PlaPlatformSettings();
            cache.setSettingsKey(RedisKeyConstant.REDIS_COMMISSION_KEY);
            cache.setSettingsValue(commission);
            cache.setSettingsName("抽成比例");
            cache.setCreateBy(userId.toString());
            cache.setUpdateBy(userId.toString());
            baseMapper.insert(cache);
        }else{
            cache.setUpdateBy(userId.toString());
            cache.setSettingsValue(commission);
            baseMapper.updateById(cache);
        }
        redisCache.setCacheObject(RedisKeyConstant.REDIS_COMMISSION_KEY,commission);
        return ApiResult.success();
    }
    /**
     * 查看是否需要支付保证金
     * @param type 类型1-汽配商入驻,2-维修厂入驻,3-司机入驻
     * @return
     */
    @Override
    public Boolean isPayBaod(int type) {
        PlaPlatformSettings one = this.getOne(new LambdaQueryWrapper<PlaPlatformSettings>()
                .eq(PlaPlatformSettings::getSettingsKey, RedisKeyConstant.REDIS_BOND_KEY));
        if(one==null){
            throw new GlobalException("后台平台设置中未设置保证金");
        }
        BondDTO bondDTO = JSONObject.parseObject(one.getSettingsValue(), BondDTO.class);
        switch (type) {
            case 1://汽配商入驻
                if(bondDTO.getAutoPartsPay()==1){//汽配商入驻是否需要缴纳 0：否 1:是
                    return true;
                }
                break;


        }
            return false;
    }

}
